const mkapp = require(config.mkapp);
const db = require(config.db_url);
const express = require('express');
const router = express.Router();
let reg = require(`${config.user_model}reg.sql`);

//注册
router.get('/',async(req, res, next)=>{
	res.render(`${config.user_template}repass`);
});


router.post('/',async(req, res, next)=>{
	try {
		let body = req.body;
		
		let pass =  mkapp.md5(body.pass +  mkapp.MD5_SUFFIX);
	
		let smscode = req.body.smscode;
		let tel = body.tel;

		let reTel = await db(res,reg.repeatTel,[tel]);

        if(reTel.length == 0){
			res.json({success:false,msg:'此手机号不存在'});
            return;
        }

		
		if(smscode != req.session.smscode){
			res.json({success:false,msg:'对不起，您输入的验证码不正确哦^_^'});
            return
		}


		let update = await db(res,reg.updatePassByPhone,[pass,tel]);

		if(update){
			res.json({success:true,msg:'密码修改成功'});
            return
		}
		
	} catch (error) {
		console.error(error);
	}
});
module.exports = router;